package com.example.mymall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mymall.entity.GoodsEntity;
import com.example.mymall.entity.UserEntity;
import com.example.mymall.entity.UserGoodsCartVO;
import com.example.mymall.repository.impl.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author <a href="https://srpihot.site">Srpihot</a>
 * @since 2021-10-06
 */
@RestController
@RequestMapping("/api/goods")
public class GoodsController {
    @Autowired
    private GoodsServiceImpl goodsService;

    @RequestMapping(value = "/findAll", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public List<GoodsEntity> findAll(){
        return goodsService.list(null);
    }

    @RequestMapping(value = "/findById", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public GoodsEntity findById(int id){
        return goodsService.getById(id);
    }

    @RequestMapping(value = "/page", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Page<GoodsEntity> page(int currentPage, int pageSize){
        return goodsService.selectGoodsPage(currentPage,pageSize);
    }

    @RequestMapping(value = "/del", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> DelById(int id){
        Map<String,Object> res = new HashMap<String,Object>();
        if(goodsService.removeById(id)){
            res.put("msg","success");
            res.put("data","数据删除成功~");
        }else{
            res.put("msg","error");
            res.put("data","数据删除失败~");
        }
        return res;
    }

    @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> AddGoods(GoodsEntity goods){
        Map<String,Object> res = new HashMap<String,Object>();
        try{
            if(goodsService.saveOrUpdate(goods)){
                res.put("msg","success");
                res.put("data","数据添加成功~");
            }else{
                res.put("msg","error");
                res.put("data","数据添加失败~");
            }
        } catch (Exception e){
            res.put("msg","error");
            res.put("data",e.toString());
        }

        return res;
    }

    @RequestMapping(value = "/update", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> UpdateByEntity(GoodsEntity goods){
        Map<String,Object> res = new HashMap<String,Object>();
        int id = goods.getId();
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        try{
            if(goodsService.update(goods,queryWrapper)){
                res.put("msg","success");
                res.put("data","数据删除成功~");
            }else{
                res.put("msg","error");
                res.put("data","数据删除失败~");
            }
        } catch (Exception e){
            res.put("msg","error");
            res.put("data",e.toString());
        }
        return res;
    }
}

